package android_testsuite.mytest.camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android_testsuite.R;
import android_testsuite.mytest.util.PreferenceHelper;

/**
 * Created by renhui on 2015/4/24.
 */
public class CameraIntentTestActivity extends Activity {
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;

    private Button mCameraBtn;
    private ImageView mImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_camera_intent_test);
        mCameraBtn = (Button) findViewById(R.id.camera_button);
        mImageView = (ImageView) findViewById(R.id.camera_img);
        try {
            int screenChange =  Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
            //Toast.makeText(this, screenChange == 1 ? "重力感应开" : "重力感应关", Toast.LENGTH_LONG).show();
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }

        int apiLevel = Build.VERSION.SDK_INT;
        Toast.makeText(this, "" + apiLevel, Toast.LENGTH_LONG).show();

        mCameraBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                Uri uri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                PreferenceHelper.setImageUri(CameraIntentTestActivity.this, uri.getPath());
                intent.putExtra(MediaStore.EXTRA_OUTPUT , uri);
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        Log.e("111", resultCode + "");
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                String uri = PreferenceHelper.getImageUri(this);
                // TODO 可能会出OOM
                Bitmap  bitmap = BitmapFactory.decodeFile(uri);
                mImageView.setImageBitmap(bitmap);
            } else if (resultCode == RESULT_CANCELED) {
                Log.e("111", "用户已经取消图片抓取");
            } else {
                Log.e("111", "what are you doing???");
            }
        }
    }

    private static Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
            Log.e("111", mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }
}
